import React from "react";
import { sleep } from 'antd-mobile/es/utils/sleep';
import styles from '@/assets/css/order.module.css';
import type OrderType from "@/types/order";

const Index = () =>
{
    const [active,SetActive] = useState<string>('all');
    const [list,SetList] = useState<OrderType[]>([]);
    const [HasMore,SetHasMore] = useState<boolean>(true);
    const [page,SetPage] = useState<number>(1);

    // 按钮结构
    const ButtonList = (order:OrderType) => 
    {
        if(order.status == '3') //未支付
        {
            return (
                <React.UI.Button color='warning' size='small' onClick={() => React.navigate(`/order/rate?orderid=${order.id}`)}>立即评价
                </React.UI.Button>
            )
        }else if(order.status == '1') //已支付
        {
            return (
              <React.UI.Button color='danger' size='small'>申请退款</React.UI.Button>
            )
        }
    }

    // 选项卡的切换
    const OnTabChange = (value:string) =>
    {
        SetActive(value);
        OnRefresh();
    }

    // 下拉刷新的事件
    const OnRefresh = async () =>
    {
        await sleep(1500);
        SetList([]);
        SetPage(1);
        SetHasMore(true);
    }

    // 上拉加载
    const OnLoadMore = async () =>
    {
        await sleep(1500);
        await GetOrderData();
    }

    const GetOrderData = async () =>
    {
        const params = {
            busid:React.business.id,
            page,
            status:active
        }

        const result = await React.http.post('/order/index',params);

        if(result.code === 1)
        {
            const DataCount:number = result.data.count;
            let ListCount:number = 0;

            SetList((v) =>
            {
                const data = [...v,...result.data.list];

                ListCount = data.length;

                return data;
            });

            if(ListCount >= DataCount)
            {
                SetHasMore(false);

                return false;
            }

            SetPage(num => num + 1);
        }else{
            SetHasMore(false);

            React.fail(result.msg);
        }
    }

    return (
        <>
            {/* 选项卡切换 */}
            <React.UI.Tabs onChange={OnTabChange} style={{backgroundColor: '#fff'}} activeKey={active} activeLineMode={'full'}>
                <React.UI.Tabs.Tab title="全部" key="all" />
                <React.UI.Tabs.Tab title="未支付" key="0" />
                <React.UI.Tabs.Tab title="已支付" key="1" />
                <React.UI.Tabs.Tab title="已入住" key="2" />
                <React.UI.Tabs.Tab title="已退房" key="3" />
                <React.UI.Tabs.Tab title="申请退款" key="-1" />
            </React.UI.Tabs>

            <div style={{height:'10px'}}></div>

            {/* 下拉刷新列表 */}
            <React.UI.PullToRefresh onRefresh={OnRefresh}>
                {/* 订单列表 */}
                <div className={styles.order_list}>
                    {list && list.map((item: any, key: number) =>
                        <div className={styles.item} key={key}>
                            <div className={styles.item_top}>
                                <p>{item.room.name}</p>
                                <div className={styles.top_tag}>{item.status_text}</div>
                            </div>

                            <div className={styles.house}>
                                <div className={styles.item_swipers}>
                                    <img src={item.room.thumb_text} alt="" />
                                </div>
                                <div className={styles.item_times}>
                                    <div>
                                        <div>{item.starttime_text}</div>
                                        <div>{item.startday_text}</div>
                                    </div>
                                    <div>
                                        <div>共{item.order_day}晚</div>
                                        <div className={styles.item_right}>
                                            <img src={'/assets/images/right1.png'} alt="" />
                                        </div>
                                    </div>
                                    <div>
                                        <div>{item.endtime_text}</div>
                                        <div>{item.endday_text}</div>
                                    </div>
                                    <div>
                                        <div className={styles.item_pay}>订单总价</div>
                                        <div className={styles.item_price}>￥{item.price}</div>
                                    </div>
                                </div>
                            </div>

                            <div className={styles.item_bar}>
                                
                                {ButtonList(item)}

                                <React.UI.Button color='primary' size='small' onClick={() => React.navigate(`/order/info?orderid=${item.id}`)}>
                                    查看详情
                                </React.UI.Button>
                            </div>
                        </div>
                    )}
                </div>

                {/* 上拉加载 */}
                <React.UI.InfiniteScroll loadMore={OnLoadMore} hasMore={HasMore} threshold={0} />
            </React.UI.PullToRefresh>
        </>
    );
}

export default Index;